home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 5 / MacMania 5.toast / / Internet software / NewsWatcher / NW Source / Source / status.c < prev    next >
Text File  |  1997-01-09  |  9KB  |  352 lines

  1. /*----------------------------------------------------------------------------
  2.  
  3.     status.c
  4.  
  5.     This module handles the status window.
  6.     
  7.     Copyright © 1994-1997, Northwestern University.
  8.  
  9. ----------------------------------------------------------------------------*/
  10.  
  11. #include <string.h>
  12.  
  13. #include "glob.h"
  14. #include "status.h"
  15. #include "newswatcher.h"
  16. #include "dialog.h"
  17. #include "menus.h"
  18. #include "strutil.h"
  19. #include "windutil.h"
  20. #include "memutil.h"
  21. #include "wind.h"
  22. #include "help.h"
  23.  
  24.  
  25.  
  26. #define kWindowWidth    450                /* status window width */
  27. #define kWindowHeight    80                /* status window height */
  28.  
  29.  
  30.  
  31. static Str255 gStatusMsg;            /* currently displayed status message */
  32.  
  33.  
  34.  
  35. /*----------------------------------------------------------------------------
  36.     DisplayStatusMessage 
  37.     
  38.     Display a status message during a long operation.
  39.     
  40.     Entry:    msg = status message.
  41.     
  42.     Exit:    function result = error code.
  43. ----------------------------------------------------------------------------*/
  44.  
  45. OSErr DisplayStatusMessage (char *msg)
  46. {
  47.     WindowPtr wind = nil;
  48.     GrafPtr port;
  49.     Rect r;
  50.     Str255 title, buttonName, fontName;
  51.     OSErr err = noErr;
  52.     Boolean mainScreen, onOneScreen;
  53.     
  54.     GetPort(&port);
  55.     wind = MyFrontWindow();
  56.     strcpy((char*)gStatusMsg, msg);
  57.     c2pstr((char*)gStatusMsg);
  58.     
  59.     if (GetMyWindowKind(wind) != kStatus) {
  60.         GetPString(kStrStatusWindowTitle, title);
  61.         GetFontName(systemFont, fontName);
  62.         err = CreateNewWindow(kStatus, title, fontName, 12, &wind);
  63.         if (err != noErr) goto exit;
  64.         MoveWindow(wind, gPrefs.statusWindowLocn.h, gPrefs.statusWindowLocn.v, false);
  65.         SizeWindow(wind, kWindowWidth, kWindowHeight, false);
  66.         if (!WindOnScreen(wind)) {
  67.             GetDominantScreen(nil, &r, &mainScreen, &onOneScreen);
  68.             MoveWindow(wind, (r.left + r.right - wind->portRect.right) >> 1, 
  69.                 GetMBarHeight() + 40, false);
  70.         }
  71.         SetRect(&r, kWindowWidth-70, kWindowHeight-30, kWindowWidth-10, kWindowHeight-10);
  72.         GetPString(kStrStatusCancelButtonName, buttonName);
  73.         NewControl(wind, &r, buttonName, true, 0, 0, 0, pushButProc, 0);
  74.         MyShowWindow(wind);
  75.     } else {
  76.         SetPort(wind);
  77.         SetRect(&r, 10, 10, kWindowWidth-10, 40);
  78.         InvalRect(&r);
  79.     }
  80.     
  81.     SetMenusTo(kAppleOnlyAboutDisabled, 0, 0, 0, 0, 0);
  82.     GiveTime(false);
  83.     SetPort(port);
  84.     return noErr;
  85.     
  86. exit:
  87.  
  88.     DoClose(wind);
  89.     SetPort(port);
  90.     return err;
  91. }
  92.  
  93.  
  94.  
  95. /*----------------------------------------------------------------------------
  96.     DisplayStatusMessageNumber 
  97.     
  98.     Display a status message during a long operation.
  99.     
  100.     Entry:    index = index in STR# 128 resource of status message.
  101.     
  102.     Exit:    function result = error code.
  103. ----------------------------------------------------------------------------*/
  104.  
  105. OSErr DisplayStatusMessageNumber (short index)
  106. {
  107.     CStr255 msg;
  108.     
  109.     GetCString(index, msg);
  110.     return DisplayStatusMessage(msg);
  111. }
  112.  
  113.  
  114.  
  115. /*----------------------------------------------------------------------------
  116.     Activate 
  117.     
  118.     Handle an activate event for the status window.
  119.             
  120.     Entry:    wind = pointer to status window.
  121.             act = true to activate, false to deactivate
  122. ----------------------------------------------------------------------------*/
  123.  
  124. static void Activate (WindowPtr wind, Boolean act)
  125. {
  126. }
  127.  
  128.  
  129.  
  130. /*----------------------------------------------------------------------------
  131.     Update 
  132.     
  133.     Handle an update event for the status window.
  134.             
  135.     Entry:    wind = pointer to status window.
  136. ----------------------------------------------------------------------------*/
  137.  
  138. static void Update (WindowPtr wind)
  139. {
  140.     UpdateControls(wind, wind->visRgn);
  141.     TextFont(systemFont);
  142.     TextSize(12);
  143.     TruncString(kWindowWidth-20, gStatusMsg, smTruncEnd);
  144.     MoveTo(10, 29);
  145.     DrawString(gStatusMsg);
  146.     
  147.     #ifdef kDevelopmentVersion
  148.         {
  149.             char *t1 = "NewsWatcher development version.";
  150.             char *t2 = "Please do not redistribute.";
  151.         
  152.             TextFont(applFont);
  153.             TextSize(9);
  154.             MoveTo(10, 60);
  155.             DrawText(t1, 0, strlen(t1));
  156.             MoveTo(10, 71);
  157.             DrawText(t2, 0, strlen(t2));
  158.         }
  159.     #endif
  160.  
  161. }
  162.  
  163.  
  164.  
  165. /*----------------------------------------------------------------------------
  166.     Mouse 
  167.     
  168.     Handle a mouse down event in the content area of the status window.
  169.             
  170.     Entry:    wind = pointer to status window.
  171.             where = location of mouse down in local coords.
  172.             modifiers = modifiers field from event record.
  173.             
  174.     Exit:    function result = error code.
  175. ----------------------------------------------------------------------------*/
  176.  
  177. static OSErr Mouse (WindowPtr wind, Point where, short modifiers)
  178. {
  179.     short part;
  180.     ControlHandle control;
  181.  
  182.     part = FindControl(where, wind, &control);
  183.     if (part  != 0) {
  184.         if (TrackControl(control, where, nil) != 0) gCancel = true;
  185.     }
  186.     return noErr;
  187. }
  188.  
  189.  
  190.  
  191. /*----------------------------------------------------------------------------
  192.     Draggable
  193.     
  194.     Determine whether a mouse down event is on a draggable object in a 
  195.     status window.
  196.     
  197.     Entry:    wind = pointer to status window.
  198.             where = location of mouse down event, in local coordinates.
  199.             modifiers = modifiers field from event record.
  200.             
  201.     Exit:    function result = true if object is draggable.
  202. ----------------------------------------------------------------------------*/
  203.  
  204. static Boolean Draggable (WindowPtr wind, Point where, short modifiers)
  205. {
  206.     return false;
  207. }
  208.  
  209.  
  210.  
  211. /*----------------------------------------------------------------------------
  212.     Key 
  213.     
  214.     Handle a key down event for the status window.
  215.             
  216.     Entry:    wind = pointer to status window.
  217.             theChar = ASCII code of key.
  218.             theKey = keyboard code of key.
  219.             modifiers = modifiers field from event record.
  220.             
  221.     Exit:     function result = error code.
  222. ----------------------------------------------------------------------------*/
  223.  
  224. static OSErr Key (WindowPtr wind, unsigned char theChar, unsigned char theKey, short modifiers)
  225. {
  226.     ControlHandle cancelButton;
  227.     long ticks;
  228.  
  229.     if (theKey == escapeKeyCode || (modifiers & cmdKey) != 0 && theChar == '.') {
  230.         cancelButton = ((WindowPeek)wind)->controlList;
  231.         HiliteControl(cancelButton, 1);
  232.         Delay(8, &ticks);
  233.         HiliteControl(cancelButton, 0);
  234.         gCancel = true;
  235.         return noErr;
  236.     }
  237.     SysBeep(0);
  238.     return noErr;
  239. }
  240.  
  241.  
  242.  
  243. /*----------------------------------------------------------------------------
  244.     Command 
  245.     
  246.     Handle a command for the status window.
  247.             
  248.     Entry:    wind = pointer to status window.
  249.             menu = the menu.
  250.             item = the item.
  251.             modifiers = modifiers field from event record.
  252.     
  253.     Exit:    function result = error code.
  254. ----------------------------------------------------------------------------*/
  255.  
  256. static OSErr Command (WindowPtr wind, short menu, short item, short modifiers)
  257. {
  258.     return noErr;
  259. }
  260.  
  261.  
  262.  
  263. /*----------------------------------------------------------------------------
  264.     Close 
  265.     
  266.     Close the status window.
  267.             
  268.     Entry:    wind = pointer to status window.
  269.     
  270.     Exit:    function result = error code.
  271. ----------------------------------------------------------------------------*/
  272.  
  273. static OSErr Close (WindowPtr wind)
  274. {
  275.     TWindow **info;
  276.     GrafPtr port;
  277.     
  278.     GetPort(&port);
  279.     SetPort(wind);
  280.     SetPt(&gPrefs.statusWindowLocn, 0, 0);
  281.     LocalToGlobal(&gPrefs.statusWindowLocn);
  282.     info = (TWindow**)GetWRefCon(wind);
  283.     MyDisposeHandle(info);
  284.     MyDisposeWindow(wind);
  285.     SetPort(port);
  286.     return noErr;
  287. }
  288.  
  289.  
  290.  
  291. /*----------------------------------------------------------------------------
  292.     Idle 
  293.     
  294.     Handle idle time tasks for a status window.
  295.             
  296.     Entry:    wind = pointer to status window.
  297.     
  298.     Exit:    cursorRgn = cursor region for WaitNextEvent mouse moved events.
  299. ----------------------------------------------------------------------------*/
  300.  
  301. static void Idle (WindowPtr wind, RgnHandle cursorRgn)
  302. {
  303. }
  304.  
  305.  
  306.  
  307. /*----------------------------------------------------------------------------
  308.     Help 
  309.     
  310.     Handle help balloons for a status window.
  311.             
  312.     Entry:    wind = pointer to status window.
  313.             where = current mouse location in local coordinates.
  314. ----------------------------------------------------------------------------*/
  315.  
  316. static void Help (WindowPtr wind, Point where)
  317. {
  318.     Rect r;
  319.     Point tip = {0,0};
  320.  
  321.     SetRect(&r, wind->portRect.right-70, wind->portRect.bottom-30, 
  322.         wind->portRect.right-10, wind->portRect.bottom-10);
  323.     if (PtInRect(where, &r)) ShowHelpBalloon(tip, &r, 9);
  324. }
  325.  
  326.  
  327.  
  328. /*----------------------------------------------------------------------------
  329.     InitStatusDispatchTable 
  330.     
  331.     Initialize the dispatch table for the status window.
  332. ----------------------------------------------------------------------------*/
  333.  
  334. void InitStatusDispatchTable (void)
  335. {
  336.     TDispatch *d;
  337.     
  338.     d = &gDispatch[kStatus];
  339.     
  340.     d->activate = Activate;
  341.     d->update = Update;
  342.     d->mouse = Mouse;
  343.     d->draggable = Draggable;
  344.     d->key = Key;
  345.     d->grow = nil;
  346.     d->zoom = nil;
  347.     d->command = Command;
  348.     d->close = Close;
  349.     d->idle = Idle;
  350.     d->help = Help;
  351. }
  352.